<template>
  <el-container>
    <el-header>
      <el-row :gutter="10">
        <el-col :span="4">
          <div class="grid-content bg-purple logo">
            <img src="/static/logo.png" alt>
          </div>
        </el-col>
        <el-col :span="16">
          <div class="grid-content bg-purple title">电商后台管理系统</div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple logout">
            <el-button type="warning" @click="loginOut">退出</el-button>
          </div>
        </el-col>
      </el-row>
    </el-header>
    <el-container>
      <!-- 折叠时64px,展开是174px -->
      <el-aside :width="isCollapse ? '64px' : '174px'">
        <!-- 添加折叠div -->
        <div class="toggle-button" @click="toggleCollapse()">|||</div>
        <el-menu
          unique-opened
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
        >
          <el-submenu v-for="item in menus" :key="item.id" :index="item.id+''">
            <!-- 一级菜单 -->
            <template slot="title">
              <!-- 一级菜单的图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 一级菜单的内容 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              v-for="item1 in item.children"
              :key="item1.id+''"
              :index="'/'+item1.path"
            >
              <template slot="title">
                <!-- 二级菜单的图标 -->
                <i class="el-icon-menu"></i>
                <!-- 二级菜单的内容 -->
                <span>{{item1.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 二级路由 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { leftMemus } from '@/api/menu.js'
export default {
  data() {
    return {
      menus: [],
      // 顶级菜单的图标数据
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 菜单是否折叠
      isCollapse: false
      // 当前激活的路由
     // activePath: this.$route.path
    }
  },
  created() {
    // 获取角色列表
    this.$store.dispatch('role/getRoleListSync')
    // this.activePath = sessionStorage.getItem('path')
    this.getLeftMenus()
  },
  methods: {
    async getLeftMenus() {
      const { meta, data } = await leftMemus()
      if (meta.status !== 200) {
        return this.$message.error(meta.msg)
      }
      this.menus = data
    },
    // 折叠和显示菜单
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存激活的路由地址
    /* saveActivePath(path) {
      this.activePath = path
      sessionStorage.setItem('path', path)
    }, */
    // 退出登录
    loginOut() {
      sessionStorage.removeItem('token')
      this.$message.success('退出成功')
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="scss" scoped>
.el-container {
  height: 100vh;
  .el-header {
    padding-left: 0;
    background-color: #d3dce6;
    color: #333;
    line-height: 60px;
    .logo {
      height: 60px;
    }
    .title {
      text-align: center;
      color: white;
      font-size: 24px;
    }
    .logout {
      text-align: right;
    }
  }
  .el-aside {
    background-color: #333744;
    color: #333;
    text-align: left;
    line-height: 200px;
    //按钮样式
    .toggle-button {
      background-color: #4a5064;
      font-size: 10px;
      line-height: 24px;
      color: #fff;
      text-align: center;
      letter-spacing: 0.3em;
      cursor: pointer;
    }
    .el-menu {
      border-right: 0;
      .iconfont {
        margin-right: 8px;
      }
    }
  }
  .el-main {
    background-color: #e9eef3;
  }
}
</style>
